package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.ChessBoard;
import com.sghd.fight.module.game.core.Grid;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.SkillReport;
import com.sghd.pojo.fight.game.TargetReport;
import com.sghd.pojo.fight.game.UnitValue;
import com.sghd.pojo.fight.game.ValueItem;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @author frank
 * @date 2018-12-12 16:58
 * @desc 虫洞
 **/
@Component
public class WormholeEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        ChessBoard chessBoard = room.getChessBoard();
        int col = chessBoard.getCol(), row = chessBoard.getRow();
        int index = -1;
        while (true) {
            index = RandomUtils.nextInt(col * row);
            int x = index / col;
            int y = index % row;
            if (x != 0 && x != col - 1 && y != 0 && y != row - 1) {
                break;
            }
        }
        Set<Integer> bomms = chessBoard.boom(index);
        for (Integer idx : bomms) {
            Grid grid = chessBoard.getGrids()[idx];
            //owner.getMagic().collect(grid.getType());//收集
            grid.remove();//
        }
        int attack = bomms.size();
        attack = (int) (attack * getBet(owner, target));
        attack = fixHp(owner, target, attack, room, config);

        context.addValue(target, UnitValue.HP, -attack);//扣血
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -attack));

        chessBoard.getEliminated().addAll(bomms);
        chessBoard.fill();
        //自动消除
        autoElimi(room, owner, skillReport, chessBoard);
        skillReport.getIndexs().add(index);
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_WORMHOLE;
    }
}
